<?php
/**
* designer.php
*
* Create on 2015-12-2
* Create by TT
*/
namespace haibao\cms\business;

use haibao\cms\data\Designer as DesignerData;
use haibao\cms\model\data\Designer\Designer as DesignerModel;
use haibao\cms\model\data\Designer\DesignerRelBrand as DesignerRelBrandModel;
use \haibao\cms\model\filter\Designer\Designer as DesignerFilter;
use \haibao\frame\data\query\Condition ;
use \haibao\cms\common\ChineseSpell;
use \haibao\cms\business\BusinessException ;
use \haibao\cms\model\data\Tag as TagModel;

class Designer extends \haibao\cms\business\Base{
	public static $designerGender = array(
			DesignerModel::GENDER_MALE =>'男性',
			DesignerModel::GENDER_FEMALE =>'女性',
			DesignerModel::GENDER_NEUTRAL =>'中性',
	);
	
	public static $designerArea = array(
			DesignerModel::PlACE_EUROPE_AMERICA =>'欧美',
			DesignerModel::PlACE_JAPAN =>'日本',
			DesignerModel::PlACE_KOREA =>'韩国',
			DesignerModel::PlACE_HK_TAIWAN_SOUTHEAST_ASIA =>'港台/东南亚',
			DesignerModel::PlACE_CHINA =>'内地',
			DesignerModel::PlACE_OTHER =>'其他',
	);
	
	public static $designerFace = array(
			DesignerModel::FACE_CHINA =>'中国设计师',
			DesignerModel::FACE_CHINESE_ORIGIN =>'华裔设计师',
	);
	
	public static $designerIdentity = array(
			DesignerModel::IDENTITY_CUSTUME_DESIGN =>'服装设计',
			DesignerModel::IDENTITY_JEWELRY_DESIGN =>'珠宝配饰设计',
			DesignerModel::IDENTITY_CELEBRITY =>'明星',
			DesignerModel::IDENTITY_ART_DESIGN =>'造型艺术设计',
			DesignerModel::IDENTITY_OTHER =>'其他',
	);
	
	public static $designerSiteState = array(
			DesignerModel::SITE_STATE_FANS_MORE =>'最受关注',
			DesignerModel::SITE_STATE_NEW_ONLINE =>'最新上线',
	);
	public static $designerRelBrand = array(
	    DesignerRelBrandModel::RELTYPE_PERSON =>'个人品牌 ',
	    DesignerRelBrandModel::RELTYPE_NOW =>'正在掌管品牌 ',
	    DesignerRelBrandModel::RELTYPE_OLD =>'曾经掌管品牌 ',
	);
	
	public static $designerXingZuo = array(
	    DesignerModel::BAIYANG => '白羊座',
	    DesignerModel::SHUANGZI => '双子座',
	    DesignerModel::JINNIU => '金牛座',
	    DesignerModel::JUXIE => '巨蟹座',
	    DesignerModel::SHIZI => '狮子座',
	    DesignerModel::CHUNV => '处女座',
	    DesignerModel::TIANPING => '天秤座',
	    DesignerModel::TIANXIE => '天蝎座',
	    DesignerModel::SHESHOU => '射手座',
	    DesignerModel::MOJIE => '魔羯座',
	    DesignerModel::SHUIPING => '水瓶座',
	    DesignerModel::SHUANGYU => '双鱼座',
	);
	public static function getBrands($brandsArr = null){
		$data = new DesignerData();
		return $data->getBrands($brandsArr);
	}
	
	public static function getAllBrands(){
		$data = new DesignerData();
		return $data->getAllBrands();
	}
	
	public static function getBrandsByLetter($letterLow){
		$data = new DesignerData();
		return $data->getBrandsByLetter($letterLow);
	}
	
	/**
	 * 通过品牌ID和类型 获取关联设计师
	 * @param array/string $brandId 
	 * @param  $type 个人(\haibao\cms\model\data\designer\DesignerRelBrand::RELTYPE_PERSON) 
	 * @param  $type 正在掌管(\haibao\cms\model\data\designer\DesignerRelBrand::RELTYPE_NOW) 
	 * @param  $type 曾经(\haibao\cms\model\data\designer\DesignerRelBrand::RELTYPE_OLD) 
	 * @return multitype:NULL
	 */
	public static function getDesignerByBrandId($brandId,$type = array()){
		$data = new DesignerData();
		return $data->getDesignerByBrandId($brandId,$type);
	}
	
	public static function getDesignerListCount(){
		$data = new DesignerData();
		return $data->getDesignerListCount();
	}
	
	public static function addDesignerClickNum($designerId){
		$data = new DesignerData();
		$data->addDesignerClickNum($designerId);
	}
	
	/**
	 * 通过设计师ID获取粉丝数 EditFansNum
	 * 	@param $designerId 设计师ID
	 *  @param $type 类型 点击量：\haibao\cms\model\data\Brand::TYPE_CLICKNUM 粉丝数：\haibao\cms\model\data\Brand::TYPE_FANSNUM
	 */
	public static function getDeisgnerFansNumByDesignerId($designerId,$type){
		if(!$designerId){
			throw new BusinessException('设计师ID不能为空');
		}
		$data = new DesignerData();
		return $data->getDeisgnerFansNumByDesignerId($designerId,$type);
	}
	
	/**
	 * 修改设计师粉丝数,点击量
	 *  @param $editFansNum 点击量/粉丝数
	 *  @param $designerId 设计师ID
	 *  @param $type 类型 点击量：\haibao\cms\model\data\Brand::TYPE_CLICKNUM 粉丝数：\haibao\cms\model\data\Brand::TYPE_FANSNUM
	 */
	public static function editDesignerFansNumByDesignerId($editFansNum,$designerId,$type){
		if(!isset($editFansNum)){
			throw new BusinessException('修改处不能为空');
		}
		if(!$designerId){
			throw new BusinessException('设计师ID不能为空');
		}
		$data = new DesignerData();
		return $data->editDesignerFansNumByDesignerId($editFansNum,$designerId,$type);
	}
	
	/**
	 * 修改评论数
	 * @param unknown_type $designerId
	 * @param unknown_type $userId
	 * @param unknown_type $num
	 * @param unknown_type $isSub
	 * @throws \haibao\cms\business\BusinessException
	 */
	public static function updateCommentNum($designerId,$userId,$num=1,$isSub=false){
		if(!$designerId){
			throw new \haibao\cms\business\BusinessException('Id不能为空');
		}
		$data = new DesignerData();
		$data->updateCommentNum((int)$designerId,$userId,$num,$isSub);
	}
	
	public static function getDesignerByFilter($filter,$isGetOne = true){
	    $designerData = new DesignerData();
	    if($isGetOne){
	        $data = $designerData->getOne($filter);
	    }else{
	        $data = $designerData->getAll($filter);
	    }
	    return $data;
	}
	
	public static function getDesignerByIds($ids,$pubBrands = null,$isMessage = false){
		$designerArr = array();
		if($pubBrands){
			if(is_array($ids) && empty($ids)){
				return $designerArr;
			}
		}else{
			if(empty($ids)){
				throw new BusinessException('设计师ID不存在！',self::DATA_EMPTY);
			}
		}
		$filter = new \haibao\cms\model\filter\designer\Designer();
		$filter->select(array(
				\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_TOPIC_ID,
				\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_NAMECN,
				\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_NAMEEN,
				\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_IMGID,
		));
		if(is_array($ids)){
			$filter->where(\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_IN,$ids);
		}else {
			$filter->where(\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$ids);
		}
		
		$filter->where(\haibao\cms\model\filter\designer\Designer::CONDITION_FIELD_NAME_STATUS, \haibao\frame\data\query\Condition::CONDITION_EQUAL, \haibao\cms\model\data\designer\Designer::STATUS_ENABLE);
	
		$data = new DesignerData();
		$designerList = $data->getAll($filter);
		
	    foreach($designerList as $list){
            if($isMessage){
                $designerArr[$list->Id]['Title'] = $list->NameCN.'('.$list->NameEN.')';
                /* if(self::$isPc){} */
                $designerArr[$list->Id]['Url'] =  \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_DESIGNER).'/designer/'.$list->TopicId.'/';
            }else{
              $designerArr[$list->Id] = $list;
	        }
        }
		return $designerArr;
	}
	
	/**
	 * 获取设计师Info通过tagId
	 * @param unknown $tagId
	 * @return Ambigous <NULL, unknown>
	 */
	public static function getDesignerInfoByTagId($tagId){
		$designerInfo = null;
		if($tagId){
			$designerData = new DesignerData();
			$designerFilter = new DesignerFilter();
			$designerFilter->select(array(
					DesignerFilter::CONDITION_FIELD_NAME_ID,
					DesignerFilter::CONDITION_FIELD_NAME_TAG_ID,
					DesignerFilter::CONDITION_FIELD_NAME_NAMECN,
					DesignerFilter::CONDITION_FIELD_NAME_NAMEEN,
					DesignerFilter::CONDITION_FIELD_NAME_IMGID,
					DesignerFilter::CONDITION_FIELD_NAME_BIRTHDAY,
					DesignerFilter::CONDITION_FIELD_NAME_IDENTITY,
					DesignerFilter::CONDITION_FIELD_NAME_CATEGORY_TAGID,
					DesignerFilter::CONDITION_FIELD_NAME_XINGZUO,
					DesignerFilter::CONDITION_FIELD_NAME_GENDER,
					DesignerFilter::CONDITION_FIELD_NAME_REGION,
					DesignerFilter::CONDITION_FIELD_NAME_DESCRIPTION,
			));
			$designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_TAG_ID, Condition::CONDITION_EQUAL, $tagId);
			$designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_STATUS, \haibao\frame\data\query\Condition::CONDITION_EQUAL, \haibao\cms\model\data\designer\Designer::STATUS_ENABLE);
			$designerInfo = $designerData->getOne($designerFilter);
		}
		return $designerInfo;
	}
	
	public static function getDesignerInfoByTagIds($tagIds){
	    $designerInfo = null;
	    if($tagIds){
	        $designerData = new DesignerData();
	        $designerFilter = new DesignerFilter();
	        $designerFilter->select(array(
	            DesignerFilter::CONDITION_FIELD_NAME_ID,
	            DesignerFilter::CONDITION_FIELD_NAME_TAG_ID,
	            DesignerFilter::CONDITION_FIELD_NAME_TOPIC_ID,
	            DesignerFilter::CONDITION_FIELD_NAME_NAMECN,
	            DesignerFilter::CONDITION_FIELD_NAME_NAMEEN,
	        ));
	        $designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_TAG_ID, Condition::CONDITION_IN, $tagIds);
	        $designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_STATUS, \haibao\frame\data\query\Condition::CONDITION_EQUAL, \haibao\cms\model\data\designer\Designer::STATUS_ENABLE);
	        $designerInfo = $designerData->getAll($designerFilter);
	    }
	    return $designerInfo;
	}
	
	/**
	 * 添加设计师
	 * @param : \haibao\cms\model\data\designer
	 */
	public static function addDesigner($designerModel){
		if(!$designerModel){
			throw new BusinessException('设计师不能为空',self::DATA_EMPTY);
		}
		if(!$designerModel->NameCN){
			throw  new BusinessException('设计师中文名不能为空',self::DATA_EMPTY_NAME);
		}else{
			$chineseSpell = new ChineseSpell();
			$chSpellFull  = $chineseSpell->getFullSpell($designerModel->NameCN," ");
			$designerModel->PinyinCN = strtolower(substr($chSpellFull,0,1));
		}
	
		if(self::hasSameDesignerName($designerModel->NameCN, null)){
			throw new BusinessException('设计师中文名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		if(!$designerModel->NameEN){
			$designerModel->NameEN = $chSpellFull;
		}
        $designerModel->PinyinEN = strtoupper(substr($designerModel->NameEN,0,1));
	
		if(!$designerModel->Description){
			$designerModel->Description = '';
		}
		if($designerModel->Status == \haibao\cms\model\data\designer\Designer::STATUS_ENABLE){
			$tagInfo = self::insertDesignerTag($designerModel);
			$designerModel->TagId = $tagInfo->Id;
			$designerModel->TopicId = $tagInfo->TopicId;
		}
		$designerModel->CreateTime = $designerModel->UpdateTime = new \DateTime();
		$designerData = new \haibao\cms\data\Designer;
		$resultInsert = $designerData->addDesigner($designerModel);
		
		if($designerModel->ArtistInsideSecrecy){	
			$designerModel->ArtistInsideSecrecy->ArtistId = $resultInsert->Id;
			$designerModel->ArtistInsideSecrecy->ArtistName = $resultInsert->NameCN;
			\haibao\cms\business\artist\ArtistInsideSecrecy::saveArtistInsideSecrecy($designerModel->ArtistInsideSecrecy);
		}
		
		return $resultInsert;
	}
	
	/**
	 * 判断设计师中文名、设计师英文名是否已经存在
	 * @param unknown $nameCN
	 * @param unknown $nameEN
	 * @param string $brandId
	 * @throws BusinessException
	 * @return boolean
	 */
	public static function hasSameDesignerName($nameCN,$nameEN,$designerId = null,$isStatus = TRUE){
	   
		$designerFilter = new DesignerFilter();
		if(!$nameCN && !$nameEN){
			throw new BusinessException('设计师名称不能为空',self::DATA_EMPTY_NAME);
		}
		if($nameCN){
			$designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_NAMECN, Condition::CONDITION_EQUAL, $nameCN);
		}
		if($nameEN){
			$designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_NAMEEN, Condition::CONDITION_EQUAL, $nameEN);
		}
		if($designerId){
			$designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $designerId);
		}
		if($isStatus){
		    $designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\designer\Designer::STATUS_ENABLE);
		}
		$designerData = new DesignerData();
		$designerInfo = $designerData->getOne($designerFilter);
		if($designerInfo){
			return $designerInfo->Id;
		}else{
			return false;
		}
	}

	public static function hasSameDesignerNamForValidate($nameCN){

		$designerFilter = new DesignerFilter();
		if(!$nameCN){
			throw new BusinessException('设计师名称不能为空',self::DATA_EMPTY_NAME);
		}
		if($nameCN){
			$designerFilter->where(DesignerFilter::CONDITION_FIELD_NAME_NAMECN, Condition::CONDITION_EQUAL, $nameCN);
		}
		$designerData = new DesignerData();
		$designerInfo = $designerData->getOne($designerFilter);
		if($designerInfo){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 *  根据标签ID更新品牌信息
	 * @param \haibao\cms\model\data\designer $designerModel
	 * @throws BusinessException
	 */
	public static function UpdateDesignerByTagId($designerModel){
		$deisgnerData = new \haibao\cms\data\Designer();
		$designerModel->UpdateTime = new \DateTime();
		$arrSet = array(
				DesignerFilter::CONDITION_FIELD_NAME_NAMECN =>$designerModel->NameCN,
				DesignerFilter::CONDITION_FIELD_NAME_NAMEEN =>$designerModel->NameEN,
				DesignerFilter::CONDITION_FIELD_NAME_STATUS=> $designerModel->Status,
				DesignerFilter::CONDITION_FIELD_NAME_UPDATETIME => $designerModel->UpdateTime
		);
		$filter = new \haibao\cms\model\filter\designer\Designer();
		$filter->where(DesignerFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_EQUAL,$designerModel->TagId);
		$deisgnerInfo = $deisgnerData->getOne($filter);
		if($deisgnerInfo && self::hasSameDesignerName($deisgnerInfo->NameCN, null,$deisgnerInfo->Id)){
			throw new BusinessException('设计师中文名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		$designerModel->Id = $deisgnerInfo->Id;
		$deisgnerData->updateDesignerByFilter($arrSet,$filter,$designerModel);
	}
	
	public  static function getDesignerInfoById($designerId){
	    $data = new \haibao\cms\data\Designer();
	    return $data->getDesignerInfoById($designerId);
	}
	
	public static function editDesigner($designerModel){
	    self::checkOperation(self::FUNC_CODE_BRAND_EDIT_DESIGENR);
	    self::checkCelebrity($designerModel);
	    $celebrityData = new \haibao\cms\data\Designer();
		if($designerModel->Status == \haibao\cms\model\data\designer\Designer::STATUS_ENABLE && !$designerModel->TagId){
			$tagInfo = self::insertDesignerTag($designerModel);
			$designerModel->TagId = $tagInfo->Id;
			$designerModel->TopicId = $tagInfo->TopicId;
		}
	    $celebrityData->updateDesignerById($designerModel);
	    
	    if($designerModel->ArtistInsideSecrecy){
	    	$designerModel->ArtistInsideSecrecy->ArtistId = $designerModel->Id;
	    	$designerModel->ArtistInsideSecrecy->ArtistName = $designerModel->NameCN;
	    	\haibao\cms\business\artist\ArtistInsideSecrecy::saveArtistInsideSecrecy($designerModel->ArtistInsideSecrecy,true);
	    }
	    self::saveOperateHistory(self::FUNC_CODE_BRAND_EDIT_DESIGENR, array('id'=>$designerModel->Id));
	}
	private static function checkCelebrity($designerModel){
	    if(empty($designerModel->NameCN) && empty($designerModel->NameEN)){
	        throw new BusinessException('设计师中文/英文名不能为空',self::DATA_EMPTY_NAME);
	    }
	    
	    $hasSameCnName = \haibao\cms\business\Designer::hasSameDesignerName($designerModel->NameCN, '',$designerModel->Id,false);
	    if($hasSameCnName){
	        throw new BusinessException('设计师中文名已存在',self::DATA_EMPTY_NAME);
	    }
	    $hasSameNnName = \haibao\cms\business\Designer::hasSameDesignerName('', $designerModel->NameEN,$designerModel->Id,false);
	    
	    if($hasSameNnName){
	        throw new BusinessException('设计师英文名称已存在',self::DATA_EMPTY_NAME);
	    }
	}
	
	public static function delDesignerById($id,$arrSet){

	    self::checkOperation(self::FUNC_CODE_BRAND_DELETE_DESIGENR);
	    
	    if (!$id){
	    
	        throw new BusinessException('设计师ID不能为空');
	    }
	    $deisgnerData = new \haibao\cms\data\Designer();
	    
	    $deisgnerData->delDesignerById($id,$arrSet);
	    
	    self::saveOperateHistory(self::FUNC_CODE_BRAND_DELETE_DESIGENR, array($id,$arrSet));
	}
	
	public static function changeDesignerStatus($id,$arrSet){
	    $deisgnerData = new \haibao\cms\data\Designer();
	     
	    $deisgnerData->delDesignerById($id,$arrSet);
	}
	
	public static function getDesignerProperty($designerId){
		if(!$designerId){
			throw new \haibao\cms\business\BusinessException('Id不能为空');
		}
		$data = new \haibao\cms\data\Designer();
		return $data->getDesignerProperty($designerId);
	
	}
	
	public static function getAllDesignerIdByWeight(){
		$deisgnerData = new \haibao\cms\data\Designer();
		return $deisgnerData->getAllDesignerIdByWeight();
	}

	public static function insertDesignerTag($model){
		$tagModel = new \haibao\cms\model\data\Tag();
		$tagData = new \haibao\cms\data\Tag();

		$tagModel->TagName  = $model->NameCN;
		$tagModel->Alias = $model->NameCN;
	    $tagModel->ParentId = TagModel::DESINGER_TAG_ID;
		$tagModel->TagLevel = 2;
		$tagModel->QuanPin = $model->NameCN;
		$tagModel->JianPin = $model->NameEN;
		$tagModel->TagType = TagModel::TYPE_DESIGNER;
		$tagModel->TagStatus = TagModel::STATUS_ENABLE;
		$tagModel->Creator =  self::getCurrentUser()->UserName;
		$tagModel->CreateTime = time();
		$tagModel->LastModified = $tagModel->Creator;
		$tagModel->TopId = TagModel::DESINGER_TAG_ID;
		$tagModel->TopicId = $tagData->getMaxTopicId() + 1;
		$tagModel->Suser =  $tagModel->Creator;

		\haibao\cms\business\Tag::setTagUrl($tagModel);
		$tagData->add($tagModel);
		//$tagData->clearCache();
		return $tagModel;
	}
	
	public static function getLibraryDesignerRecommend(){
	    $deisgnerData = new \haibao\cms\data\Designer();
		return $deisgnerData->getLibraryDesignerRecommend();
	}
	
}